﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace WeatherGod
{
    public class XML
    {
        static string xmlFile = "Config.xml";

        #region xml
        public static string Read(string rootnode, string node)
        {
            if (string.IsNullOrEmpty(rootnode))
            {
                rootnode = "Settings";
            }

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFile);
            XmlNodeList nodeList = xmlDoc.SelectSingleNode(rootnode).ChildNodes;
            foreach (XmlNode xn in nodeList)
            {
                XmlNodeList xcn = xn.ChildNodes;
                foreach (XmlNode xcn2 in xcn)
                {
                    if (xcn2.Name == node)
                    {
                        return xcn2.InnerText;
                    }
                }
            }
            return string.Empty;
        }
        public static string ReadExt(string root, string node, string childNode)
        {
            if (string.IsNullOrEmpty(root))
            {
                root = "Settings";
            }

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFile);
            //XmlNodeList nodeList = xmlDoc.SelectSingleNode("Settings").ChildNodes;
            XmlNodeList nodeList = xmlDoc.SelectSingleNode(root).ChildNodes;
            foreach (XmlNode xn in nodeList)
            {
                if (xn.Name == node)
                {
                    XmlNodeList xcn = xn.ChildNodes;
                    foreach (XmlNode xcn2 in xcn)
                    {
                        if (xcn2.Name == childNode)
                        {
                            return xcn2.InnerText;
                        }
                    }
                }
            }
            return string.Empty;
        }
        //WriteXML("AccessToken", "abcdef");
        public static void Write(string node, string value)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFile);
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("Settings").ChildNodes;
            foreach (XmlNode xn in nodeList)
            {
                foreach (XmlNode xn2 in xn)
                {
                    if (xn2.Name == node)
                    {
                        xn2.InnerText = value;
                        xmlDoc.Save(xmlFile);
                        break;
                    }
                }
            }
            xmlDoc.Save(xmlFile);
        }
        public static void WriteExt(string node, string chindNode, string value)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFile);
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("Settings").ChildNodes;
            foreach (XmlNode xn in nodeList)
            {
                if (xn.Name == node)
                {
                    foreach (XmlNode xn2 in xn)
                    {
                        if (xn2.Name == chindNode)
                        {
                            xn2.InnerText = value;
                            xmlDoc.Save(xmlFile);
                            break;
                        }
                    }
                }
            }
            xmlDoc.Save(xmlFile);
        }


        #endregion
    }
}
